package com.smart.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLUtil {
	public static final String PUSH_MANAGER_ORDER_PARAM_KEY="ARGS";
	public static String loadJson (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        	e.printStackTrace();
        } catch (IOException e) {
        	e.printStackTrace();
        }
        return json.toString();
    }
	
	public static String loadJsonUTF8 (String url, String type){
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            HttpURLConnection httpConn = (HttpURLConnection)urlObject.openConnection();
            httpConn.setRequestMethod(type);
            BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"utf-8"));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        	e.printStackTrace();
        } catch (IOException e) {
        	e.printStackTrace();
        }
        return json.toString();
    }
	public static String loadJsonUTF8_post(String url, String pbJson){
        StringBuilder json = new StringBuilder("");
        try {
            URL urlObject = new URL(url);
            HttpURLConnection httpConn = (HttpURLConnection)urlObject.openConnection();
            httpConn.setDoOutput(true);   //需要输出
            httpConn.setDoInput(true);   //需要输入
            httpConn.setUseCaches(false);  //不允许缓存
            httpConn.setRequestMethod("POST");
            httpConn.connect();
            PrintWriter writer=new PrintWriter(httpConn.getOutputStream());
            writer.write(PUSH_MANAGER_ORDER_PARAM_KEY+"="+pbJson);
            writer.flush();
            writer.close();
            int resultCode=httpConn.getResponseCode();
            if(HttpURLConnection.HTTP_OK==resultCode){
            	  BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"utf-8"));
                  String inputLine = null;
                  while ( (inputLine = in.readLine()) != null) {
                      json.append(inputLine);
                  }
                  in.close();
            }
          
        } catch (Exception e) {
        	System.out.println(url+"========================"+pbJson);
        	e.printStackTrace();
        }
        return json.toString();
    }
}
